<!--出卷-->
<template>
  <div>
     <el-card style="height: 680px;">
       <el-form :model="DataForm" status-icon ref="DataForm" :rules="dataForm_rules" label-width="120px" class="demo-ruleForm" style="margin-top: 25px;">
         <el-form-item label="科目：" prop="f_sb_id">
           <div class="block">
             <el-cascader
               placeholder="请选择"
               :options="options"
               filterable
               v-model="DataForm.f_sb_id"
             ></el-cascader>
           </div>
         </el-form-item>
         <el-row>
           <el-col :span="8">
             <el-form-item label="考卷标题:" prop="ep_name">
                 <el-input v-model="DataForm.ep_name"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="8">
             <el-form-item label="考试时间(分钟):" prop="ep_test_time">
               <el-input-number v-model="DataForm.ep_test_time" :min="10" :max="200"></el-input-number>
             </el-form-item>
           </el-col>
         </el-row>
         <el-row>
           <el-col :span="14">
             <el-form-item label="试题描述:" prop="ep_describe">
               <el-input v-model="DataForm.ep_describe"></el-input>
             </el-form-item>
           </el-col>
         </el-row>
       </el-form>
         <el-form ref="form" :model="form" label-width="120px">
           <!--单选题-->
           <el-row>
             <el-col :span="2">
               <el-form-item label="单选题">
               </el-form-item>
             </el-col>
             <el-col :span="6">
               <el-form-item label="数量:">
                 <el-input-number v-model="form.testQuestionsList[0].tq_questions_obj.question_Num" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
             <el-col :span="8">
               <el-form-item label="每题分数:">
                 <el-input-number v-model="form.testQuestionsList[0].fra" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
           </el-row>

           <!--多选题-->
           <el-row>
             <el-col :span="2">
               <el-form-item label="多选题">
               </el-form-item>
             </el-col>
             <el-col :span="6">
               <el-form-item label="数量:">
                 <el-input-number v-model="form.testQuestionsList[1].tq_questions_obj.question_Num" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
             <el-col :span="8">
               <el-form-item label="每题分数:">
                 <el-input-number v-model="form.testQuestionsList[1].fra" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
           </el-row>

           <!--判断题-->
           <el-row>
             <el-col :span="2">
               <el-form-item label="判断题">
               </el-form-item>
             </el-col>
             <el-col :span="6">
               <el-form-item label="数量:">
                 <el-input-number v-model="form.testQuestionsList[2].tq_questions_obj.question_Num" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
             <el-col :span="8">
               <el-form-item label="每题分数:">
                 <el-input-number v-model="form.testQuestionsList[2].fra" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
           </el-row>

           <!--填空题-->
           <el-row>
             <el-col :span="2">
               <el-form-item label="填空题">
               </el-form-item>
             </el-col>
             <el-col :span="6">
               <el-form-item label="数量:">
                 <el-input-number v-model="form.testQuestionsList[3].tq_questions_obj.question_Num" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
             <el-col :span="8">
               <el-form-item label="每题分数:">
                 <el-input-number v-model="form.testQuestionsList[3].fra" :min="0" :max="100"></el-input-number>
               </el-form-item>
             </el-col>
           </el-row>
         </el-form>
         <div style="margin-left: 50px;">
           <el-button type="primary" @click="crateQuestion">生成试题</el-button>
           <el-button style="margin-left: 100px;" @click="clearQuestionInfo">重置</el-button>
         </div>
     </el-card>

  </div>
</template>
<script>
  import {getAllSubject} from '@/api/questions'
  import {addExaminationPaper} from '@/api/invigilate'
  export default{
    components: {

    },
    created(){
      this.c_getAllSubject();
    },
    data(){
      return {
         form:{
           ep_name:"", //考卷标题
           ep_test_time:10, //考试时间
           ep_fraction:0, //总分
           ep_state:0, //状态
           ep_describe:"", //描述
           testQuestionsList:[
             {  //单选
                 tq_questions_id:"",
                 fra:0, //单个题的分数
                 tq_fraction:0, //总分
                 tq_type:1,
                 tq_questions_obj:{question_Num:0,sb_id:0},
                 tq_question_info:""
             },
             {  //多选
               tq_questions_id:"",
               fra:0, //单个题的分数
               tq_fraction:0, //总分
               tq_type:2,
               tq_questions_obj:{question_Num:0,sb_id:0},
               tq_question_info:""
             },
             { //判断
               tq_questions_id:"",
               fra:0, //单个题的分数
               tq_fraction:0, //总分
               tq_type:3,
               tq_questions_obj:{question_Num:0,sb_id:0},
               tq_question_info:""
             },
             { //填空
               tq_questions_id:"",
               fra:0, //单个题的分数
               tq_fraction:0, //总分
               tq_type:4,
               tq_questions_obj:{question_Num:0,sb_id:0},
               tq_question_info:""
             }
           ]
         },
         DataForm:{
            f_sb_id:0,
            ep_name:"", //考卷标题
            ep_test_time:10, //考试时间
            ep_state:0, //状态
            ep_describe:"" //描述
         },
         num:0,
         options:[], //选择所属科目

        dataForm_rules:{
          f_sb_id:[{ required: true, message: '不能为空', trigger: 'change' }],
          ep_name:[{ required: true, message: '不能为空', trigger: 'change' }],
          ep_test_time:[{ required: true, message: '不能为空', trigger: 'change' }],
          ep_describe:[{ required: true, message: '不能为空', trigger: 'change' }],
        }
      }
    },
    methods:{
      //创建考题
      crateQuestion(){
        this.$refs["DataForm"].validate((valid) => {
            if(valid){
               this.form.ep_name = this.DataForm.ep_name;
               this.form.ep_test_time = this.DataForm.ep_test_time;
               this.form.ep_state = this.DataForm.ep_state;
               this.form.ep_describe = this.DataForm.ep_describe;

               let sum = 0;
               for(let obj of this.form.testQuestionsList){
                   obj.tq_questions_obj.sb_id = parseInt(this.DataForm.f_sb_id[2]);  //课目
                   obj.tq_fraction = obj.fra *  obj.tq_questions_obj.question_Num;  //类型题总分
                   obj.tq_question_info = JSON.stringify(obj.tq_questions_obj);
                 sum = sum + obj.tq_fraction;
               }

               this.form.ep_fraction = sum;  //试题总分

              console.log(this.form);

              addExaminationPaper(this.form).then(res=>{
                  if(res.code == 200){
                    this.$message({
                      message: '试题创建成功，请到实体管理中查看',
                      type: 'success'
                    });
                    this.DataForm.ep_name = "";

                  }
              });
            }
        })
      },

      clearQuestionInfo(){
        this.DataForm.f_sb_id = 0;
        this.DataForm.ep_name = "";
        this.DataForm.ep_test_time = 10;
        this.DataForm.ep_state = 0;
        this.DataForm.ep_describe = "";

        for(let obj of this.form.testQuestionsList){
          obj.tq_questions_obj.question_Num = 0;
          obj.fra = 0;
        }
      },

      c_getAllSubject(){
        getAllSubject().then(res=>{
          if(res.code == 200){
            for(let i = 0;i < res.data.length;i ++){
              let tLevel = {value:res.data[i].l_id,label:res.data[i].l_name};
              this.options.push(tLevel);
              if(res.data[i].list != null && res.data[i].list.length > 0){
                let gList = res.data[i].list;
                let children = [];
                for(let j = 0;j < gList.length;j ++){
                  let gGroup = {value:gList[j].g_id,label:gList[j].g_name};
                  children.push(gGroup);

                  if(gList[j].list != null && gList[j].list.length > 0){
                    let sList = gList[j].list;
                    let children1 = [];

                    for(let n = 0;n < sList.length;n ++){
                      let subject = {value:sList[n].sb_id,label:sList[n].sb_name};
                      children1.push(subject);
                    }
                    gGroup.children = children1;
                  }
                }
                tLevel.children = children;
              }
            }
          }
        });
      },
    }
  }
</script>
